﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Web;
using Slam.FieldTypes;
using SlamCms.Common;

namespace SlamCms.SharePoint.FieldTypes
{
	public partial class FilteredLookupFieldEditor : UserControl, IFieldEditor
	{
		private string lookupList;
		private string filterQuery;
		private string lookupFieldName;
		private bool defaultToFirstValue;
		private string fieldValueType;

		#region IFieldEditor Members

		public bool DisplayAsNewSection
		{
			get { return true; }
		}

		protected override void CreateChildControls()
		{
			base.CreateChildControls();

			ddlLookupFieldTargetList.AutoPostBack = true;
			ddlLookupFieldTargetList.SelectedIndexChanged += new EventHandler(ddlLookupFieldTargetList_SelectedIndexChanged);
		}

		protected void ddlLookupFieldTargetList_SelectedIndexChanged(object sender, EventArgs e)
		{
			PopulateFields();
		}

		private void PopulateFields()
		{
			ddlLookupFieldTargetField.Items.Clear();
			SPSite siteCollection = SPControl.GetContextSite(HttpContext.Current);
			if (ddlLookupFieldTargetList.SelectedValue.IsGuid())
			{
				Guid listId = new Guid(ddlLookupFieldTargetList.SelectedValue);
				bool found = false;
				foreach (SPWeb site in siteCollection.AllWebs)
				{
					SPList list = null;
					try
					{
						list = site.Lists[listId];
						foreach (SPField field in list.Fields)
						{
							if (!field.Hidden)
							{
								ddlLookupFieldTargetField.Items.Add(new ListItem(field.Title, field.InternalName));
							}
						}
					}
					catch { }

					site.Dispose();
					if (list != null)
						break;
				}
			}
		}

		public void InitializeWithField(Microsoft.SharePoint.SPField field)
		{
			SPSite siteCollection = SPControl.GetContextSite(HttpContext.Current);

			foreach (SPWeb site in siteCollection.AllWebs)
			{
				foreach (SPList list in site.Lists)
				{
					if (!list.Hidden)
					{
						if (String.IsNullOrEmpty(site.Name))
						{
							ddlLookupFieldTargetList.Items.Add(new ListItem(list.Title, list.ID.ToString()));
						}
						else
						{
							ddlLookupFieldTargetList.Items.Add(new ListItem(site.Name + " - " + list.Title, list.ID.ToString()));
						}
					}
				}
				site.Dispose();
			}

			if (!Page.IsPostBack)
			{
				ddlLookupFieldSaveValue.Items.Add(new ListItem("Lookup Value"));
				ddlLookupFieldSaveValue.Items.Add(new ListItem("Item ID"));
				ddlLookupFieldSaveValue.Items.Add(new ListItem("Item Text"));
				ddlLookupFieldSaveValue.Items.Add(new ListItem("Item SLAM ID"));

				SlamField lookupField = field as SlamField;
				if (lookupField != null)
				{
					lookupList = (string)lookupField.GetCustomPropertyValue("LookupList");
					filterQuery = (string)lookupField.GetCustomPropertyValue("FilterQuery");
					lookupFieldName = (string)lookupField.GetCustomPropertyValue("LookupField");
					defaultToFirstValue = (bool)lookupField.GetCustomPropertyValue("DefaultToFirstValue");
					fieldValueType = (string)lookupField.GetCustomPropertyValue("FieldValueType");
				}

				ListItem selectedTargetListItem = ddlLookupFieldTargetList.Items.FindByValue(lookupList);
				if (selectedTargetListItem != null)
				{
					selectedTargetListItem.Selected = true;
					txtQuery.Text = filterQuery;
					cbDefaultToFirstValue.Checked = defaultToFirstValue;

					PopulateFields();

					selectedTargetListItem = ddlLookupFieldTargetField.Items.FindByValue(lookupFieldName);
					if (selectedTargetListItem != null)
					{
						selectedTargetListItem.Selected = true;
					}
				}

				ListItem selectedValueTypeItem = ddlLookupFieldSaveValue.Items.FindByValue(fieldValueType);
				if (selectedValueTypeItem != null)
				{
					selectedValueTypeItem.Selected = true;
				}
				else
				{
					ddlLookupFieldSaveValue.SelectedIndex = 0;
				}
			}
		}

		public void OnSaveChange(Microsoft.SharePoint.SPField field, bool isNewField)
		{	
			SlamField lookupField = field as SlamField;

			lookupField.SetCustomProperty("LookupList", typeof(string), ddlLookupFieldTargetList.SelectedValue);
			lookupField.SetCustomProperty("LookupField", typeof(string), ddlLookupFieldTargetField.SelectedValue);
			lookupField.SetCustomProperty("FilterQuery", typeof(string), txtQuery.Text);
			lookupField.SetCustomProperty("DefaultToFirstValue", typeof(bool), cbDefaultToFirstValue.Checked);
			lookupField.SetCustomProperty("FieldValueType", typeof(string), ddlLookupFieldSaveValue.SelectedValue);
		}

		#endregion

	}
}
